#ifndef __LED_H__
#define __LED_H__

#include <REGX52.H>
#include <stdint.h>
#include "delay.h"

#define SPK_PORT P2
#define SPK_BIT 0
#define LED_PORT P2
#define LED_RED 1
#define LED_GREEN 2

#define TUNE_RIGHT 523
#define TUNE_WRONG 262
#define LEDSPK_TIME 1

#define SPK_BITMASK (1 << SPK_BIT)
#define SPK_BITMASK_NEG (NOT SPK_BITMASK)
#define LED_BITMASK ((1 << LED_RED) | (1 << LED_GREEN))

#define SPK_FREQ_TO_RELOAD(freq) (uint16_t)(0x10000 - SYS_CLOCK / (freq) / 2 + 7)
#define _SPK_TIME_TO_CYCLE_HELPER(freq, time) (uint16_t)((freq) * (time) * 2)
#define SPK_TIME_TO_CYCLE(freq, time) (\
	((_SPK_TIME_TO_CYCLE_HELPER((freq), (time)) + 255) >> 8) | \
	(_SPK_TIME_TO_CYCLE_HELPER((freq), (time)) & 0xFF) \
)

void LEDSPK_Init(void);
void LEDSPK_Flash(uint8_t ledmask_neg, uint16_t reload, uint16_t cycle);

#endif
